package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.InputPwdActivity;
import flc.ast.activity.LocalPlayActivity;
import flc.ast.activity.PictureFormatActivity;
import flc.ast.activity.SetPwdActivity;
import flc.ast.adapter.MovieAdapter;
import flc.ast.databinding.FragmentPlayBinding;
import flc.ast.utils.Content;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PlayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PlayFragment extends BaseNoModelFragment<FragmentPlayBinding> {

    private MovieAdapter movieAdapter;
    private int type = 0;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.fragment_play;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivLocalPlay.setOnClickListener(this);
        mDataBinding.ivPrivacyVideo.setOnClickListener(this);
        mDataBinding.tvHot.setOnClickListener(this);
        mDataBinding.tvClassic.setOnClickListener(this);
        mDataBinding.tvLike.setOnClickListener(this);

        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext));
        movieAdapter = new MovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);

        mDataBinding.refreshTabItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshTabItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshTabItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getHotData();
                } else if (type == 1) {
                    getClassicData();
                } else if (type == 2) {
                    getLikeData();
                }
                mDataBinding.refreshTabItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getHotData();
                } else if (type == 1) {
                    getClassicData();
                } else if (type == 2) {
                    getLikeData();
                }
                mDataBinding.refreshTabItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivLocalPlay://本地播放
                getPermission();
                break;
            case R.id.ivPrivacyVideo://私密视频
                //判断是否设置密码
                boolean isHave = SPUtil.getBoolean(mContext, Content.isHavePassword, false);
                if (isHave) {
                    startActivity(InputPwdActivity.class);
                } else {
                    startActivity(SetPwdActivity.class);
                }
            case R.id.tvHot://热门
                clearSelection();
                type = 0;
                page = 1;
                mDataBinding.tvHot.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvHot.setTextSize(22);
                getHotData();
                break;
            case R.id.tvClassic://经典重映
                clearSelection();
                type = 1;
                page = 1;
                mDataBinding.tvClassic.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvClassic.setTextSize(22);
                getClassicData();
                break;
            case R.id.tvLike://猜你喜欢
                clearSelection();
                type = 2;
                page = 1;
                mDataBinding.tvLike.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvLike.setTextSize(22);
                getLikeData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
    }

    @Override
    protected void initData() {
        getHotData();
    }

    private void getHotData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            movieAdapter.setList(result);
                        } else {
                            movieAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getClassicData() {
        StkResApi.getTagResourceList(this, AppConfig.CLASSIC_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            movieAdapter.setList(result);
                        } else {
                            movieAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getLikeData() {
        StkResApi.getTagResourceList(this, AppConfig.LIKE_DATA, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            movieAdapter.setList(result);
                        } else {
                            movieAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void getPermission() {
        StkPermissionHelper.reqManageExternalStoragePermission().reqPermissionDesc(getString(R.string.get_local_video_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                startActivity(LocalPlayActivity.class);
            }
        }).request();
    }

    private void clearSelection() {
        mDataBinding.tvHot.setTextColor(Color.parseColor("#666666"));
        mDataBinding.tvHot.setTextSize(14);
        mDataBinding.tvClassic.setTextColor(Color.parseColor("#666666"));
        mDataBinding.tvClassic.setTextSize(14);
        mDataBinding.tvLike.setTextColor(Color.parseColor("#666666"));
        mDataBinding.tvLike.setTextSize(14);
    }

}